import numpy as np
from PIL import Image
img = Image.open('grid.bmp')

pix = np.array(img)
print(pix)
amp = 2.0
ret = np.zeros((int(pix.shape[0]*amp),int(pix.shape[1]*amp)))

def S(x):
    if abs(x)>=0 and abs(x)<1:
        return 1-2*abs(x)**2+abs(x)**3
    elif abs(x)>=1 and abs(x)<2:
        return 4-8*abs(x)+5*abs(x)**2-abs(x)**3
    elif abs(x)>=2:
        return 0

for i in range(ret.shape[0]):
    for j in range(ret.shape[1]):
        cor = (i/amp,j/amp)
        cor_int = (int(cor[0]),int(cor[1]))
        cor_frag = (cor[0]-cor_int[0],cor[1]-cor_int[1])
        u=cor_frag[0]
        v=cor_frag[1]

        if cor_int[0]-1>=0 and cor_int[1]-1>=0 and cor_int[0]+2<=pix.shape[0]-1 and cor_int[1]+2<=pix.shape[1]-1:
            B = pix[cor_int[0]-1:cor_int[0]+3,cor_int[1]-1:cor_int[1]+3]
            C = np.array((S(v+1),S(v),S(v-1),S(v-2))).reshape((4,1))
            A = np.array((S(u+1),S(u),S(u-1),S(u-2)))
            _ = np.dot(B,C)
            ret[i,j]=np.dot(A,_)
        else:
            ret[i,j]=0
img_=Image.fromarray(ret)
img_.show()

